package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Supplier;
import com.ldatum.crm.service.SupplierService;
import com.ldatum.crm.serviceImpl.SupplierServiceImpl;
@ParentPackage("admin")
public class SupplierAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private SupplierService supplierService = new SupplierServiceImpl();
	private List<Supplier> supplierList =new ArrayList<Supplier>();
	private Supplier supplier = new Supplier();
	
	private int id;
	private String name;
	private String lxr;
	private String phone;
	private String address;
	private String fax;
	private String email;
	private String remarks;
	private String date;
	private String ids;
	
	
	/**
	 * 
	 * 描述 :新建供应商
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		
		return "addSupplier";
	}
	
	/**
	 * 保存供应商
	 * @throws Exception3
	 */
	public void save() throws Exception{
		supplier.setAddress(address);
		supplier.setEmail(email);
		supplier.setFax(fax);
		supplier.setLxr(lxr);
		supplier.setName(name);
		supplier.setPhone(phone);
		supplier.setDate(date);
		supplier.setRemarks(remarks);
		supplier.setId(id);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		int result;
		if("".equals(id) || id == 0){
			result = supplierService.save(supplier);
		}else{
			result = supplierService.update(supplier);
		}
		if(result==1){
			json.accumulate("success", true);
		}else{
			json.accumulate("success", false);
		}
		out.println(json.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 
	 * 描述 :药品列表
	 * @return
	 * @throws Exception 
	 */
	public String list() throws Exception{
		supplierList = supplierService.list();
		return "listSupplier";
	}
	
	/**
	 * 
	 * 描述 : 供应商信息
	 * @return
	 * @throws Exception 
	 */
	public String edit() throws Exception{
		supplier = supplierService.findById(id);
		id = supplier.getId();
		return "addSupplier";
	}
	
	/**
	 * 
	 * 描述 : <描述函数实现的功能>. <br>
	 * @return
	 * @throws Exception 
	 */
	public void deletes() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		supplierService.delete(ids);
		json.accumulate("success", true);
		out.println(json.toString());
		out.flush();
		out.close();
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLxr() {
		return lxr;
	}

	public void setLxr(String lxr) {
		this.lxr = lxr;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	
	
	
}
